package com.derbysoft.lowcostadapter.commons.redis.service.impl;

import com.derbysoft.lowcostadapter.commons.model.RoomStay;
import com.derbysoft.lowcostadapter.commons.redis.service.JedisClusterService;
import com.derbysoft.lowcostadapter.commons.redis.service.RedisSerializable;
import com.derbysoft.lowcostadapter.commons.redis.service.RoomStayCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by : jason
 * DateTime: 12-2-10
 */
@Component("roomStayCacheService")
public class RoomStayCacheServiceImpl implements RoomStayCacheService {

    @Autowired
    private JedisClusterService jedisClusterService;

    @Override
    public List<RoomStay> loadAll(String key) {
        List<RoomStay> roomStays = new ArrayList<RoomStay>();
        Map<String, RedisSerializable> roomStaysMap = jedisClusterService.hGetAll(key);
        for (Map.Entry<String, RedisSerializable> entry : roomStaysMap.entrySet()) {
            roomStays.add((RoomStay) entry.getValue());
        }
        return roomStays;
    }

    @Override
    public void save(RoomStay roomStay) {
        jedisClusterService.hset(roomStay);
    }

    @Override
    public void saveAll(List<RoomStay> roomStays) {
        jedisClusterService.pipelined(roomStays);
    }
}
